arcdps\extras\message/npc.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
use crate::util::str_from_cstr_len;
use std::ffi::c_char;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// An NPC chat message.
///
/// Strings are available for the duration of the call.
/// If you need it for longer than that, consider converting it to [`NpcMessageOwned`].
///
/// ```no_run
/// # use arcdps::extras::{NpcMessage, NpcMessageOwned};
/// # let message: &NpcMessage = todo!();
/// let owned = message.to_owned();
/// let owned: NpcMessageOwned = message.into();
/// ```
#[derive(Debug, Clone)]
#[repr(C)]
pub struct NpcMessage {
/// Null terminated character name of the NPC that sent the message.
///
/// The string is only valid for the duration of the call.
character_name: *const c_char,
character_name_length: u64,
/// Null terminated string containing the content of the message that was sent.
///
/// The string is only valid for the duration of the call.
text: *const c_char,
text_length: u64,
/// Time since epoch in nanoseconds.
///
/// This can be used to sort messages, when they are out of order.
pub timestamp: u64,
}
impl NpcMessage {
/// Converts the message to its owned counterpart.
#[inline]
pub fn to_owned(&self) -> NpcMessageOwned {
self.into()
}
/// Returns the character name of the player that sent the message.
#[inline]
pub fn character_name(&self) -> &str {
unsafe { str_from_cstr_len(self.character_name, self.character_name_length) }
}
/// Returns the character name as raw pointer.
#[inline]
pub fn character_name_ptr(&self) -> *const c_char {
self.character_name
}
/// Returns the account name length.
#[inline]
pub fn character_name_len(&self) -> usize {
self.character_name_length as _
}
/// Returns the text content of the message.
#[inline]
pub fn text(&self) -> &str {
unsafe { str_from_cstr_len(self.text, self.text_length) }
}
/// Returns the text as raw pointer.
#[inline]
pub fn text_ptr(&self) -> *const c_char {
self.text
}
/// Returns the account name length.
#[inline]
pub fn text_len(&self) -> usize {
self.text_length as _
}
}
/// [`NpcMessage`] with owned [`String`] fields.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NpcMessageOwned {
/// Character name of the NPC that sent the message.
pub character_name: String,
/// Content of the message.
pub text: String,
}
impl From<NpcMessage> for NpcMessageOwned {
#[inline]
fn from(msg: NpcMessage) -> Self {
(&msg).into()
}
}
impl From<&NpcMessage> for NpcMessageOwned {
#[inline]
fn from(msg: &NpcMessage) -> Self {
Self {
character_name: msg.character_name().to_owned(),
text: msg.text().to_owned(),
}
}
}